//
//  HomeViewController.m
//  budejiePersonalPage
//
//  Created by 李叶夫 on 16/8/25.
//  Copyright © 2016年 李叶夫. All rights reserved.
//
/*
 帖子:
 http://s.budejie.com/topic/user-topic/12615952/1/desc/bs0315-iphone-4.3/0-20.json
                      topic/user-topic/12615952/1/desc/bs0315-iphone-4.3/0-20.json
 
 分享:
 http://s.budejie.com/topic/share-topic/13930724/bs0315-iphone-4.3/0-20.json
 
 评论:
 http://s.budejie.com/comment/user-comment/13930724/bs0315-iphone-4.3/0-20.json
 */

#import "HomeViewController.h"
#import "PersonalPageTableViewCell.h"
#import "ContentModel.h"
#import "HeaderViewController.h"
#import "HeaderModel.h"

#define TOPICUSER @"topic/user-topic/"
#define TOPICSHARE @"topic/share-topic/"
#define COMMENTUSER @"comment/user-comment/"
#define FOOTER @"/bs0315-iphone-4.3/0-20.json"
#define TOPICUSERFOOTER @"/1/desc/bs0315-iphone-4.3/0-20.json"

@interface HomeViewController ()<HeaderModelDelegate>

@property (strong, nonatomic) NSMutableArray *topicDataArr;

@property (strong, nonatomic) NSMutableArray *shareDataArr;

@property (strong, nonatomic) NSMutableArray *commentDataArr;

@property (strong, nonatomic) HeaderModel *headerModel;

@property (strong, nonatomic) HeaderViewController *headerVc;

//保存当前点击了那个按钮的标记
@property (assign, nonatomic) NSInteger tag;
@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.uid = @"13930724";
//    self.uid = @"17652677";
    
    
    HeaderViewController *headerVc = [[HeaderViewController alloc] initWithNibName:@"HeaderViewController" bundle:nil];
    
    self.tableView.tableHeaderView = headerVc.view;
    
    self.headerVc = headerVc;
    headerVc.delegate = self;
    
    self.tableView.estimatedRowHeight = 200;
    
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    self.tableView.contentInset = UIEdgeInsetsMake(-200, 0, 0, 0);
    
//    self.tableView.contentSize = CGSizeMake(0, 200);

    
    
}




- (instancetype)initWithStyle:(UITableViewStyle)style
{
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)setUid:(NSString *)uid
{
    _uid = uid;
    
    //拼接URL
    NSString *urlstr = [NSString stringWithFormat:@"%@%@%@",TOPICUSER,uid,TOPICUSERFOOTER];
//    NSLog(@"%@",urlstr);
    [ContentModel loadDataWithUrlstr:urlstr successBlock:^(NSArray *arr) {
        
        self.topicDataArr = [NSMutableArray arrayWithArray:arr];
        [self.tableView reloadData];
        
    } failBlock:^(NSError *error) {
        NSLog(@"error:%@",error);
    }];
    
    [HeaderModel loadDataWithUid:uid successBlock:^(HeaderModel *model) {
        
        self.headerVc.model = model;
        [self.tableView reloadData];
    } failBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
    
}


#pragma mark - tableview的代理方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//    switch (self.tag) {
//        case 0:
//            return self.topicDataArr.count;
//            
//        case 1:
//            return self.shareDataArr.count;
//            
//        default:
//            return self.commentDataArr.count;
//
//    }
    return self.topicDataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    PersonalPageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    PersonalPageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (cell == nil) {
        
        cell = [[[NSBundle mainBundle] loadNibNamed:@"PersonalPageTableViewCell" owner:nil options:nil] lastObject];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
//    ContentModel *model;
//    switch (self.tag) {
//        case 0:
//            model = self.topicDataArr[indexPath.section];
//            break;
//        case 1:
//            model = self.shareDataArr[indexPath.section];
//            break;
//            
//        default:
//            model = self.commentDataArr[indexPath.section];
//            break;
//    }
    ContentModel *model = self.topicDataArr[indexPath.section];

    cell.model = model;
    
    return cell;
}

 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 3;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark - HeaderModelDelegate代理

- (void)loadNewData:(UIButton *)button
{
    
    NSString *urlstr;
    switch (button.tag) {
        case 0:
        {
            NSLog(@"点击了0");
            //拼接URL
            NSString *urlstr = [NSString stringWithFormat:@"%@%@%@",TOPICUSER,self.uid,TOPICUSERFOOTER];
            //    NSLog(@"%@",urlstr);
            [ContentModel loadDataWithUrlstr:urlstr successBlock:^(NSArray *arr) {
                
                self.topicDataArr = [NSMutableArray arrayWithArray:arr];
                [self.tableView reloadData];
                
            } failBlock:^(NSError *error) {
                NSLog(@"error:%@",error);
            }];

        }
            break;
        case 1:
            
        {
            
                NSLog(@"点击了1");
            //拼接URLstr
            urlstr = [NSString stringWithFormat:@"%@%@%@",TOPICSHARE,self.uid,FOOTER];
            [ContentModel loadDataWithUrlstr:urlstr successBlock:^(NSArray *arr) {
                
                self.topicDataArr = [NSMutableArray arrayWithArray:arr];
                
                [self.tableView reloadData];
                
            } failBlock:^(NSError *error) {
                NSLog(@"error%@",error);
            }];
            
        }

            
            break;
        case 2:
            
        {
            NSLog(@"点击了2");
          
            
            //拼接URLstr
            urlstr = [NSString stringWithFormat:@"%@%@%@",COMMENTUSER,self.uid,FOOTER];
            [ContentModel loadDataWithUrlstr:urlstr successBlock:^(NSArray *arr) {
                
                self.topicDataArr = [NSMutableArray arrayWithArray:arr];
                [self.tableView reloadData];
                
            } failBlock:^(NSError *error) {
                NSLog(@"error%@",error);
            }];
            
        }

            break;
    
    }
    
    
}


#pragma mark - 懒加载
//- (NSMutableArray *)topicDataArr
//{
//    if (!_topicDataArr) {
//        _topicDataArr = [NSMutableArray array];
//    }
//    return _topicDataArr;
//}
//- (NSMutableArray *)shareDataArr
//{
//    if (!_shareDataArr) {
//        _shareDataArr = [NSMutableArray array];
//    }
//    return _shareDataArr;
//}
//- (NSMutableArray *)commentDataArr
//{
//    if (!_commentDataArr) {
//        _commentDataArr = [NSMutableArray array];
//    }
//    return _commentDataArr;
//}
@end













